import common from '../common.js'
var app = getApp()
var shopCommon = {}

// 加减商品 更新购物车数据
shopCommon.updateCart = (param) => {
	return new Promise ((resolve, reject) => {
		let postUrl = ''
		let orderTogether = uni.getStorageSync('orderTogether')
		if (orderTogether && orderTogether.store_id == app.globalData.storeInfo.store_id) {
			// 拼单购物车初始化
			param.cartid = orderTogether.cartId
			param.index = orderTogether.myindex
			postUrl = 'Cart&a=addSpellProduct'
		} else {
			if (app.globalData.cartInitEnd && app.globalData.cartInitEnd == param.store_id) {
				postUrl = 'Cart&a=add'
				param.deliverExtraPrice = app.globalData.storeGoods && app.globalData.storeGoods.selectAdd ? 1 : 0
			}
		}
		
		common.post(postUrl, param, function(data){
			console.log('更新购物车数据成功',app, data)
			saveCart(data)
			if (typeof param.number != 'undefined' && param.number == 0 && app.globalData.goodsSelect && app.globalData.goodsSelect[param.uniqueness_number]) {
				let obj = app.globalData.goodsSelect
				delete obj[param.uniqueness_number]
				app.globalData.goodsSelect = obj
			}
			resolve(app.globalData.storeGoods)
		}, '', function(err){
			console.log('更新购物车数据错误', err)
			if (err.data.errorCode == "77000001") {
				app.globalData.storeCart = ''
				app.globalData.storeGoods = ''
			}
			resolve()
		})
	})
}
// 初始化店铺购物车数据
shopCommon.initCart = () => {
	return new Promise ((resolve, reject) => {
		let param = {}
		let postUrl = ''
		let orderTogether = uni.getStorageSync('orderTogether')
		if (orderTogether && orderTogether.store_id == app.globalData.storeInfo.store_id) {
			// 拼单购物车初始化
			param = {
				store_id: orderTogether.store_id,
				cartid: orderTogether.cartId,
				index: orderTogether.myindex
			}
			postUrl = 'Cart&a=spellDetail'
		} else {
			param = {
				store_id: app.globalData.storeInfo.store_id,
				noTip: true,
				deliverExtraPrice: app.globalData.storeGoods && app.globalData.storeGoods.selectAdd ? 1 : 0
			}
			postUrl = 'Cart&a=detail'
		}
		
		common.post(postUrl, param, function (data) {
			console.log('初始化购物车数据成功', data)
			saveCart(data)
			app.globalData.cartInitEnd = param.store_id
			resolve(app.globalData.storeCart)
		}, '', function (err) {
			console.log('初始化购物车数据错误')
			app.globalData.cartInitEnd = param.store_id
			app.globalData.storeCart = ''
			app.globalData.storeGoods = ''
			resolve()
		})
	})
}

const saveCart = (data) => {
	console.log('select 8888888', app.globalData.storeGoods)
	let selectAdd = false
	if (app.globalData.storeGoods && app.globalData.storeGoods.store_id == data.store_id) {
		selectAdd = app.globalData.storeGoods.selectAdd
	} else {
		app.globalData.storeGoods = ''
	}
	app.globalData.storeCart = data
	if (!app.globalData.storeGoods) {
		app.globalData.storeGoods = {
			store_id: data.store_id,
			selectAdd: selectAdd,
			KeyList: {},
			totalList: {}
		} 
	}
	let KeyList = {}
	let totalList = {}
	console.log('购物车商品数据变化', data)
	data.goods_list.map(item => {
		KeyList[item.uniqueness_number] = item.num
		if (totalList[item.goods_id]) {
			totalList[item.goods_id] = totalList[item.goods_id] + item.num
		} else {
			totalList[item.goods_id] = item.num
		}
		return item
	})
	app.globalData.storeGoods.KeyList = KeyList
	app.globalData.storeGoods.totalList = totalList
}

// 获取规格商品提交接口参数
shopCommon.getSpecParam = (specGoods) => {
	var spec = []
	var prop = []

	if (specGoods.spec_list && specGoods.spec_list.length > 0) {
		spec = specGoods.spec_list.map(i => {
			var spec_item = {
				type: 'spec',
				name: i.name,
				spec_id: i.sid,
				id: i.id_
			};
			return spec_item
		});
	}

	if (specGoods.prop_list && specGoods.prop_list.length > 0) {
		prop = specGoods.prop_list.map(i => {
			var prop_item = {
				type: 'properties',
				data: []
			};

			for (var j = 0; j < i.val_status.length; j++) {
				if (i.val_status[j][1] === 0) {
					var obj = {
						id: j,
						list_id: i.id_,
						name: i.val_status[j][0]
					}
					prop_item.data = prop_item.data.concat(obj)
				}
			}

			return prop_item
		});
	}

	return spec.concat(prop)
}



module.exports = shopCommon